<template>
  <div class="device-status-panel">
    <div class="panel-header">
      <div class="left-section">
        <h2>
          <el-icon :size="20" style="margin-right: 6px;"><Monitor /></el-icon>
          设备状态
        </h2>
      </div>
      <div class="right-section">
        <el-button v-if="isFullscreen" type="primary" size="small" @click="exitFullscreen">
          <el-icon><Close /></el-icon>
          退出全屏
        </el-button>
      </div>
    </div>
    
    <div v-loading="loading" class="status-items">
      <div v-for="item in statusItems" :key="item.label" class="status-item" :class="item.status">
        <div class="item-icon">
          <el-icon :size="20"><component :is="item.icon" /></el-icon>
        </div>
        <div class="item-content">
          <div class="item-label">{{ item.label }}</div>
          <div class="item-value">{{ item.value }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue';
import { storeToRefs } from 'pinia';
import { useDeviceStore } from '@/stores/device';
import { ElMessage } from 'element-plus';
import {
  Connection,
  VideoPlay,
  Loading,
  Warning,
  Monitor,
  Close
} from '@element-plus/icons-vue';

const deviceStore = useDeviceStore();
const { currentDevice } = storeToRefs(deviceStore);

// 全屏状态
const isFullscreen = ref(false);

// 监听全屏变化
const handleFullscreenChange = () => {
  isFullscreen.value = !!document.fullscreenElement;
};

// 退出全屏
const exitFullscreen = async () => {
  try {
    await document.exitFullscreen();
  } catch (err) {
    ElMessage.error('退出全屏失败');
  }
};

onMounted(() => {
  document.addEventListener('fullscreenchange', handleFullscreenChange);
});

onUnmounted(() => {
  document.removeEventListener('fullscreenchange', handleFullscreenChange);
});

const statusItems = ref([
  {
    label: '连接状态',
    value: '离线',
    icon: 'Monitor',
    status: 'danger'
  },
  {
    label: '运行状态',
    value: '停止',
    icon: 'VideoPlay',
    status: 'warning'
  },
  {
    label: '故障状态',
    value: '正常',
    icon: 'Warning',
    status: 'success'
  }
]);

const loading = ref(false);

// 获取设备状态
const updateDeviceStatus = () => {
  if (!currentDevice.value) return;
  
  // 添加日志输出，记录当前设备信息
  console.log('当前设备信息:', currentDevice.value);
  console.log('设备连接状态(auth):', currentDevice.value.auth);
  console.log('设备运行状态:', currentDevice.value.runningStatus);
  console.log('设备故障状态:', currentDevice.value.faultStatus);
  
  // 修改判断条件，使用auth字段判断设备连接状态，与DeviceManager.vue保持一致
  const isOnline = currentDevice.value.auth === '上线';
  
  // 处理运行状态的数值映射（0=停止，1=运行）
  let runningStatusText = currentDevice.value.runningStatus || '未知';
  if (runningStatusText === '0' || runningStatusText === 0) {
    runningStatusText = '停止';
  } else if (runningStatusText === '1' || runningStatusText === 1) {
    runningStatusText = '运行';
  }
  
  // 直接使用当前设备的状态
  statusItems.value = [
    {
      label: '连接状态',
      value: isOnline ? '已连接' : '未连接',
      icon: 'Connection',
      status: isOnline ? 'success' : 'danger'
    },
    {
      label: '运行状态',
      value: runningStatusText,
      icon: 'VideoPlay',
      status: runningStatusText === '运行' ? 'success' : 
             runningStatusText === '停止' ? 'warning' : 'info'
    },
    {
      label: '故障状态',
      value: currentDevice.value.faultStatus || '未知',
      icon: 'Warning',
      status: currentDevice.value.faultStatus === '正常' ? 'success' : 'danger'
    }
  ];
};

// 监听当前设备变化
watch(() => currentDevice.value?.id, (newDeviceId) => {
  if (newDeviceId) {
    updateDeviceStatus();
  }
}, { immediate: true });

// 监听设备状态变化，只监听auth字段的变化，与DeviceManager.vue保持一致
watch(() => [
  currentDevice.value?.auth,
  currentDevice.value?.runningStatus,
  currentDevice.value?.faultStatus
], () => {
  updateDeviceStatus();
}, { deep: true });
</script>

<style scoped>
.device-status-panel {
  height: 100%;
  padding: 12px;
  background: linear-gradient(180deg, rgba(64, 158, 255, 0.05) 0%, rgba(64, 158, 255, 0) 100%);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
}

.panel-header {
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.left-section {
  h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--el-text-color-primary);
    display: flex;
    align-items: center;
  }
}

.right-section {
  display: flex;
  gap: 8px;
}
.status-items {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, minmax(60px, auto));
  gap: 12px;
  margin-top: 4px;
}

.status-item {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  min-height: 60px;
}

.status-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: currentColor;
}

.item-icon {
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: rgba(var(--el-color-primary-rgb), 0.1);
}

.item-content {
  flex: 1;
}

.item-label {
  font-size: 13px;
  color: var(--el-text-color-secondary);
  margin-bottom: 2px;
}

.item-value {
  font-size: 15px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}

/* 在线状态 */
.status-item.success {
  color: #67c23a;
  background: rgba(103, 194, 58, 0.1);
  .item-icon {
    background: rgba(103, 194, 58, 0.2);
  }
  .item-value {
    color: #67c23a;
  }
}

/* 离线状态 */
.status-item.danger {
  color: #f56c6c;
  background: rgba(245, 108, 108, 0.1);
  .item-icon {
    background: rgba(245, 108, 108, 0.2);
  }
  .item-value {
    color: #f56c6c;
  }
}

/* 运行状态 */
.status-item.warning {
  color: #e6a23c;
  background: rgba(230, 162, 60, 0.1);
  .item-icon {
    background: rgba(230, 162, 60, 0.2);
  }
  .item-value {
    color: #e6a23c;
  }
}

/* 未知状态 */
.status-item.info {
  color: #909399;
  background: rgba(144, 147, 153, 0.1);
  .item-icon {
    background: rgba(144, 147, 153, 0.2);
  }
  .item-value {
    color: #909399;
  }
}
</style>
